package com.mw.wmimobile.gui.fragments;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.mw.wmimobile.datastorage.ClassesDto;
import com.mw.wmimobile.gui.activities.ClassesClassActivity;
import com.wm.wmimobile.R;

public class ClassesAdapter extends ArrayAdapter<ClassesDto> {

	private Context context;
	private List<ClassesDto> classesDtoList;

	private TextView nameTextView;
	private TextView descriptionTextView;
	private TextView dateTextView;
	private TextView teacherTextView;
	
	private int[] listItemBackground = new int[] {
			R.drawable.staff_list_backgroung_1_transp,
			R.drawable.staff_list_backgroung_2_transp 
			};
	
	public ClassesAdapter(Context context, List<ClassesDto> list) {
		super(context, R.layout.classes_adapter_listitem, list);
		this.context = context;
		this.classesDtoList = list;
	}
		
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View rowView = inflater.inflate(
				R.layout.classes_adapter_listitem, parent, false);
		
		nameTextView = (TextView)
				rowView.findViewById(R.id.ClassesAdapterClassesNameTextItem);
		descriptionTextView = (TextView)
				rowView.findViewById(R.id.ClassesAdapterLocationTextItem);
		dateTextView = (TextView) 
				rowView.findViewById(R.id.ClassesAdapterTimeTextItem);
		teacherTextView = (TextView)
				rowView.findViewById(R.id.ClassesAdapterTeacherTextItem);
		
		final ClassesDto classes = classesDtoList.get(position);
		//nameStr = add.getName() == null ? "null" : add.getName();
		
		String classesString = classes.getName();
		
		Spanned spanned;
		spanned = android.text.Html.fromHtml(classesString );
		nameTextView.setText(spanned);
		
		
		String a = classes.getStartTime();
		String b = classes.getWeekday();
		spanned = android.text.Html.fromHtml("<b>"+b +" "+ a + "</b>");
		dateTextView.setText(spanned);
		
		String desc = classes.getRoom();
		spanned = android.text.Html.fromHtml(desc);
		descriptionTextView.setText(spanned);
		
		String teacher = classes.getTeacher();
		spanned = android.text.Html.fromHtml(teacher);
		teacherTextView.setText(spanned);
		
		
		int listItemBackgroundPosition = position % listItemBackground.length;
        rowView.setBackgroundResource(listItemBackground[listItemBackgroundPosition]);
        
        rowView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,
						ClassesClassActivity.class);
				intent.putExtra("codeIntent", classes.getId());
				context.startActivity(intent);
			}
		});
		
		return rowView;
	}

}
